#!/usr/bin/python

from pylab import *

x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y, z = np.sin(x), np.cos(x)

# plot(x,y)
# plot(x,z)
# show()

# create a new figure of size 8x6 points, using 80 dots per inch
figure(figsize=(8,6), dpi=80)

plot(x,y, label="$sin(x)$", color="blue", linewidth=2.0, linestyle="-")
plot(x,z, label="$cos(x)$", color="red", linewidth=2.0, linestyle="--")

# set title, label, legend and grid
title("PyPlot example")
xlabel("Time(s)")
ylabel("Volt")
# legend()
legend(loc="upper left")
grid()                          # only grid x ?? or grid y ??

# set x/y limits/ticks
# xlim(-4.0, 4.0)
xlim(x.min()*1.1, x.max()*1.1)
# xticks(np.linspace(-4,4,9, endpoint=True))
xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
    ['$-\pi$', '$-\pi/2$', '$0$', '$+\pi/2$', '$+\pi$'])
# ylim(-1.0, 1.0)
ylim(y.min()*1.1, y.max()*1.1)
# yticks(np.linspace(-1,1,11,endpoint=True))
yticks([-1, 0, +1],
       ['$-1$', '$0$', '$+1$'])



# spines ???
ax = gca()                      # get current axis
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# ax.xaxis.set_ticks_position('top')
ax.xaxis.set_ticks_position('bottom')
# ax.xaxis.set_ticks_position('both') # not valid, why ??
ax.spines['bottom'].set_position(('data', 0))

ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.spines['left'].set_position(('outward', 30)) # 30 is in points
# ax.spines['left'].set_position(('axes', 0.5)) # how to use ???
# ax.spines['left'].set_position('center')
# ax.spines['left'].set_position('zero')


#annotate some points
t = 2*np.pi/3
plot([t,t], [0, np.sin(t)], color='blue', linewidth=2.5, linestyle='--')
scatter([t,], [np.sin(t),], 50, color='blue')
annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
         xy=(t, np.sin(t)), xycoords='data',
         xytext=(+10, +30), textcoords='offset points', fontsize=16,
         arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

plot([t,t], [0, np.cos(t)], color='red', linewidth=2.5, linestyle='--')
scatter([t,], [np.cos(t),], 50, color='red')
annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
         xy=(t, np.cos(t)), xycoords='data',
         xytext=(-90, -50), textcoords='offset points', fontsize=16,
         arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))


# save figure using 72 dots per inch
savefig("sin.png", dpi=72)


# show result on screen
show()



# import numpy as np
# import matplotlib.pyplot as plt

# # x = np.linspace(0, 10, 100)
# x = np.arange(0, 10, 0.02)
# y = np.sin(x)
# z = np.cos(0.5*x)

# # plt.plot(x, np.sin(x))
# # plt.show()


# plt.figure(figsize=(8,4))
# ####################
# ## the actual plot
# # plt.plot(x,y, linewidth=4)
# plt.plot(x,y,label="$sin(x)$", color="red", linewidth=2)
# # plt.plot(x,z,"b--", label="$cos(0.5*x)$")
# plt.plot(x,z,"bo", label="$cos(0.5*x)$")
# ####################
# # the ??
# plt.title("PyPlot example")
# plt.xlabel("Time(s)")
# plt.ylabel("Volt")
# plt.legend()
# plt.ylim(-1.2, 1.2)
# # plt.xlim(0, 15)
# plt.grid()


# plt.show()



